home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayFast.frm < prev    next >
Text File  |  1999-05-27  |  9KB  |  309 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayFast 
  4.    Caption         =   "PlayFast"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.TextBox txtNumFrames 
  15.       Height          =   285
  16.       Left            =   1560
  17.       TabIndex        =   8
  18.       Text            =   "100"
  19.       Top             =   120
  20.       Width           =   375
  21.    End
  22.    Begin VB.OptionButton optRunType 
  23.       Caption         =   "Looping"
  24.       Height          =   255
  25.       Index           =   2
  26.       Left            =   360
  27.       TabIndex        =   6
  28.       Top             =   1560
  29.       Width           =   1095
  30.    End
  31.    Begin VB.OptionButton optRunType 
  32.       Caption         =   "Reversing"
  33.       Height          =   255
  34.       Index           =   1
  35.       Left            =   360
  36.       TabIndex        =   5
  37.       Top             =   1200
  38.       Width           =   1095
  39.    End
  40.    Begin VB.OptionButton optRunType 
  41.       Caption         =   "One time"
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   360
  45.       TabIndex        =   4
  46.       Top             =   840
  47.       Value           =   -1  'True
  48.       Width           =   1095
  49.    End
  50.    Begin VB.PictureBox picFrame 
  51.       AutoRedraw      =   -1  'True
  52.       AutoSize        =   -1  'True
  53.       Height          =   375
  54.       Index           =   0
  55.       Left            =   1560
  56.       ScaleHeight     =   21
  57.       ScaleMode       =   3  'Pixel
  58.       ScaleWidth      =   21
  59.       TabIndex        =   2
  60.       Top             =   1560
  61.       Visible         =   0   'False
  62.       Width           =   375
  63.    End
  64.    Begin VB.CommandButton cmdStart 
  65.       Caption         =   "Start"
  66.       Default         =   -1  'True
  67.       Enabled         =   0   'False
  68.       Height          =   375
  69.       Left            =   600
  70.       TabIndex        =   1
  71.       Top             =   2040
  72.       Width           =   855
  73.    End
  74.    Begin VB.PictureBox picCanvas 
  75.       Height          =   3810
  76.       Left            =   2040
  77.       ScaleHeight     =   250
  78.       ScaleMode       =   3  'Pixel
  79.       ScaleWidth      =   250
  80.       TabIndex        =   0
  81.       Top             =   0
  82.       Width           =   3810
  83.    End
  84.    Begin MSComDlg.CommonDialog dlgOpenFile 
  85.       Left            =   1560
  86.       Top             =   960
  87.       _ExtentX        =   847
  88.       _ExtentY        =   847
  89.       _Version        =   393216
  90.       CancelError     =   -1  'True
  91.    End
  92.    Begin VB.Label Label2 
  93.       Caption         =   "Frames to load:"
  94.       Height          =   255
  95.       Left            =   120
  96.       TabIndex        =   7
  97.       Top             =   120
  98.       Width           =   1455
  99.    End
  100.    Begin VB.Label lblResults 
  101.       Height          =   615
  102.       Left            =   120
  103.       TabIndex        =   3
  104.       Top             =   2640
  105.       Width           =   1815
  106.    End
  107.    Begin VB.Menu mnuFile 
  108.       Caption         =   "&File"
  109.       Begin VB.Menu mnuFileOpen 
  110.          Caption         =   "&Open..."
  111.          Shortcut        =   ^O
  112.       End
  113.    End
  114. End
  115. Attribute VB_Name = "frmPlayFast"
  116. Attribute VB_GlobalNameSpace = False
  117. Attribute VB_Creatable = False
  118. Attribute VB_PredeclaredId = True
  119. Attribute VB_Exposed = False
  120. Option Explicit
  121.  
  122. Private NumImages As Integer
  123. Private MaxImage As Integer
  124. Private Playing As Boolean
  125. Private NumPlayed As Long
  126. ' Run the animation forward and backward until
  127. ' Playing is False.
  128. Private Sub PlayImagesBackAndForth()
  129.     ' Start the animation.
  130.     Do While Playing
  131.         PlayImagesOnce
  132.         If Not Playing Then Exit Do
  133.         PlayImagesReversed
  134.     Loop
  135. End Sub
  136. ' Run the animation until Playing is false.
  137. Private Sub PlayImagesLooping()
  138.     ' Start the animation.
  139.     Do While Playing
  140.         PlayImagesOnce
  141.     Loop
  142. End Sub
  143. ' Run the animation once or until Playing is False.
  144. Private Sub PlayImagesOnce()
  145. Dim i As Integer
  146.  
  147.     ' Start the animation.
  148.     For i = 0 To NumImages - 1
  149.         ' Display the next frame.
  150.         picCanvas.Picture = picFrame(i).Picture
  151.         DoEvents
  152.         NumPlayed = NumPlayed + 1
  153.  
  154.         If Not Playing Then Exit For
  155.     Next i
  156. End Sub
  157. ' Run the animation reversed once or until Playing
  158. ' is False.
  159. Private Sub PlayImagesReversed()
  160. Dim i As Integer
  161.  
  162.     ' Start the animation.
  163.     For i = NumImages - 1 To 0 Step -1
  164.         ' Display the next frame.
  165.         picCanvas.Picture = picFrame(i).Picture
  166.         DoEvents
  167.         NumPlayed = NumPlayed + 1
  168.  
  169.         If Not Playing Then Exit For
  170.     Next i
  171. End Sub
  172.  
  173. ' Load the images.
  174. Private Sub LoadImages(file_name As String)
  175. Dim base As String
  176. Dim i As Integer
  177.  
  178.     ' Get the base file name.
  179.     base = Left$(file_name, Len(file_name) - 5)
  180.  
  181.     ' See how many frames the user wants to load.
  182.     If Not IsNumeric(txtNumFrames.Text) Then _
  183.         txtNumFrames.Text = Format$(10)
  184.     NumImages = CInt(txtNumFrames.Text)
  185.  
  186.     ' Create any needed picture boxes.
  187.     For i = MaxImage + 1 To NumImages - 1
  188.         Load picFrame(i)
  189.     Next i
  190.  
  191.     ' Get rid of any that are no longer needed.
  192.     For i = NumImages To MaxImage
  193.         Unload picFrame(i)
  194.     Next i
  195.     MaxImage = NumImages - 1
  196.     
  197.     ' Load the images.
  198.     On Error GoTo LoadPictureError
  199.     i = 0
  200.     Do While i < NumImages
  201.         lblResults.Caption = Format$(i + 1)
  202.         lblResults.Refresh
  203.         picFrame(i).Picture = LoadPicture(base & Format$(i) & ".bmp")
  204.         i = i + 1
  205.     Loop
  206.  
  207.     picCanvas.AutoSize = True
  208.     picCanvas.Picture = picFrame(0).Image
  209.     picCanvas.AutoSize = False
  210.     lblResults.Caption = ""
  211.     txtNumFrames.Text = Format$(NumImages)
  212.     Exit Sub
  213.     
  214. LoadPictureError:
  215.     ' We ran out of images early.
  216.     NumImages = i
  217.     txtNumFrames.Text = Format$(NumImages)
  218.     Resume Next
  219. End Sub
  220.  
  221. ' Run the animation until Playing is false.
  222. Private Sub PlayImages()
  223. Dim start_time As Long
  224. Dim stop_time As Long
  225.  
  226.     ' Start the appropriate animation.
  227.     NumPlayed = 0
  228.     start_time = GetTickCount
  229.     If optRunType(0).Value Then
  230.         PlayImagesOnce
  231.     ElseIf optRunType(1).Value Then
  232.         PlayImagesBackAndForth
  233.     Else
  234.         PlayImagesLooping
  235.     End If
  236.  
  237.     ' Display results.
  238.     stop_time = GetTickCount
  239.     lblResults.Caption = _
  240.         Format$(NumPlayed) & " frames/" & _
  241.         Format$((stop_time - start_time) / 1000#, "0.00") & _
  242.         " sec" & vbCrLf & vbCrLf & _
  243.         Format$(CSng(NumPlayed) / ((stop_time - start_time) / 1000#), "0.00") & _
  244.         " frames/sec"
  245. End Sub
  246. ' Start or stop playing.
  247. Private Sub cmdStart_Click()
  248.     If Playing Then
  249.         Playing = False
  250.         cmdStart.Caption = "Stopped"
  251.         cmdStart.Enabled = False
  252.     Else
  253.         cmdStart.Caption = "Stop"
  254.         lblResults.Caption = ""
  255.         DoEvents
  256.         Playing = True
  257.         PlayImages
  258.         Playing = False
  259.         cmdStart.Caption = "Start"
  260.         cmdStart.Enabled = True
  261.     End If
  262. End Sub
  263. Private Sub Form_Load()
  264.     dlgOpenFile.InitDir = App.Path
  265. End Sub
  266.  
  267.  
  268. ' Load new image files.
  269. Private Sub mnuFileOpen_Click()
  270. Dim file_name As String
  271.  
  272.     ' Let the user select a file.
  273.     On Error Resume Next
  274.     dlgOpenFile.FileName = "*_0.BMP"
  275.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  276.     dlgOpenFile.ShowOpen
  277.     If Err.Number = cdlCancel Then
  278.         Exit Sub
  279.     ElseIf Err.Number <> 0 Then
  280.         Beep
  281.         MsgBox "Error selecting file.", , vbExclamation
  282.         Exit Sub
  283.     End If
  284.     On Error GoTo 0
  285.  
  286.     Screen.MousePointer = vbHourglass
  287.     DoEvents
  288.  
  289.     file_name = Trim$(dlgOpenFile.FileName)
  290.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  291.         - Len(dlgOpenFile.FileTitle) - 1)
  292.     Caption = "PlayFast [" & dlgOpenFile.FileTitle & "]"
  293.  
  294.     ' Load the pictures.
  295.     On Error GoTo LoadError
  296.     LoadImages file_name
  297.     On Error GoTo 0
  298.  
  299.     cmdStart.Enabled = True
  300.     Screen.MousePointer = vbDefault
  301.     Exit Sub
  302.  
  303. LoadError:
  304.     Screen.MousePointer = vbDefault
  305.     MsgBox "Error " & Format$(Err.Number) & _
  306.         " opening file '" & file_name & "'" & vbCrLf & _
  307.         Err.Description
  308. End Sub
  309.